package com.wuyoucar.controller;

import com.wuyoucar.common.PageBean;
import com.wuyoucar.common.ResultBean;
import com.wuyoucar.entity.TReport;
import com.wuyoucar.entity.TUser;
import com.wuyoucar.service.IReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.UUID;


@RestController
@RequestMapping("/report")
public class ReportController {

    @Autowired
    private IReportService iReportService;

    //获取未处理的报案单
    @RequestMapping("/reviewed")
    public ResultBean getReport(@RequestParam(required = false, defaultValue = "1") int pageIndex,
                                @RequestParam(required = false, defaultValue = "2") int pageSize) {
        return iReportService.getReport(pageIndex, pageSize);
    }

    //获取勘察人员名单
    @RequestMapping("/getDispatcher")
    public ResultBean getDispatcher() {
        return iReportService.getDisPatcher();
    }

    //派遣勘察人员
    @RequestMapping("/toDispatcher/{reportid}/{userid}")
    public ResultBean toDispatcher(@PathVariable("reportid") Integer reportid, @PathVariable("userid") Integer userid) {
        System.out.println(reportid);
        System.out.println(userid);

        return iReportService.toDispatcher(reportid, userid);
    }

    //修改订单状态（初审）
    @RequestMapping("/getReportbyNum")
    public ResultBean getReportbyNum(Integer policynum, Integer firstid) {
        return iReportService.updateReportAudit(policynum, firstid);
    }

    //修改订单状态（复审）
    @RequestMapping("/updateRecheck")
    public ResultBean updateRecheck(Integer policynum, Integer firstid) {
        return iReportService.updateRecheck(policynum, firstid);
    }

    //修改订单状态（待放款）
    @RequestMapping("/passVerification")
    public ResultBean passVerification(Integer policynum, Integer firstid) {
        return iReportService.passVerification(policynum, firstid);
    }

    //修改订单状态（已驳回）
    @RequestMapping("/turndown")
    public ResultBean turndown(Integer policynum, Integer firstid, String remark) {
        return iReportService.turndown(policynum, firstid, remark);
    }


    //显示派遣成功的报案单
    @RequestMapping("/showDispatcher")
    public ResultBean showDispatcher(@RequestParam(required = false, defaultValue = "1") int pageIndex,
                                     @RequestParam(required = false, defaultValue = "1") int pageSize) {
        return iReportService.showDispatcher(pageIndex, pageSize);
    }


    //查看已通过查勘的所有申请
    @RequestMapping("/getLossAssessment")
    public ResultBean getLossAssessment() {
        return iReportService.getLossAssessment();
    }

    //查看已通过查勘的所有申请
    @RequestMapping("/getLossAssessment1")
    public ResultBean getLossAssessment1(int pageIndex, int pageSize) {
        return iReportService.getLossAssessment(pageIndex, pageSize);
    }

    //定损条件查询
    @PostMapping("/condition")
    public ResultBean condition(int pageIndex, int pageSize, @RequestBody TReport tReport) {
        return iReportService.condition(pageIndex, pageSize, tReport);
    }

    //报案人查询
    @RequestMapping("/searchName")
    public PageBean<TReport> searchName(Integer pageIndex, Integer pageSize, @RequestBody TUser tUser) {
        return iReportService.searchName(pageIndex, pageSize, tUser.getUsername());
    }

    //定损
    @PostMapping("/updateLossAssessment")
    public ResultBean updateLossAssessment(@RequestBody TReport tReport) {
        return iReportService.updateLossAssessment(tReport);
    }

    //查看报案详情
    @GetMapping("/getReportDetails")
    public ResultBean getReportDetails(Integer reportid) {
        return iReportService.getReportDetails(reportid);
    }

    /**
     * 曾海祥
     * 勘察部分
     */
    //查看已派遣的报案号
    @RequestMapping("/getHasSent/{surveyorid}")
    public ResultBean getDispatch(@PathVariable("surveyorid") Integer surveyorid) {
        return iReportService.getHasSent(surveyorid);
    }

    //查看已派遣的报案号带分页
    @RequestMapping("/page/{pageIndex}/{pageSize}/{surveyorid}")
    public PageBean<TReport> page(
            @PathVariable("pageIndex") Integer pageIndex,
            @PathVariable("pageSize") Integer pageSize,
            @PathVariable("surveyorid") Integer surveyorid) {
        return iReportService.page(pageIndex, pageSize, surveyorid);
    }

    //闪赔
    @RequestMapping(value = "/sanPei/{reportid}/{money}", method = RequestMethod.POST)
    public ResultBean uploadFile(@RequestParam("files") MultipartFile[] files,
                                 @PathVariable("reportid") Integer reportid,
                                 @PathVariable("money") Integer money,
                                 HttpServletRequest request) {
        String[] sign = {"damage", "sign"};
        int i = 0;
        String damageImg = "";  //车辆受损图名称
        String signImg = "";    //签名图片名称
        for (MultipartFile file : files) {
            //得到文件的原始文件
            String originalFilename = file.getOriginalFilename();
            //得到文件上传的路径
            final String UPLOAD_PATH_PREFIX = "static/uploadFile/";
            String realPath = new String("client/src/main/resources/" + UPLOAD_PATH_PREFIX);
            long s = new Date().getTime();
            //文件名称
            String str = sign[i++] + s + "" + originalFilename;
            System.out.println("i = " + i);
            if (i == 1) {
                damageImg = str;
            }
            if (i == 2) {
                signImg = str;
            }
            File newpath = new File(realPath);
            //构建真实的文件路径
            File filepath = new File(newpath.getAbsoluteFile() + File.separator + str);

            try {
                file.transferTo(filepath);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (damageImg.equals("") && signImg.equals("") && money == 0) {
            return new ResultBean(0, "请上传车损图,签字照片,及填写协商的理赔金额");
        } else {
            //闪赔提交
            return iReportService.sanPei(damageImg, signImg, money, reportid);
        }
    }


    //上传清晰照片,添加备注,修改状态
    @RequestMapping("/upload/{reportid}/{remark}")
    public ResultBean survey(@RequestParam(value = "img", required = false) MultipartFile img,
                             @PathVariable("reportid") Integer reportid,
                             @PathVariable("remark") String remark,
                             HttpServletRequest request) {
        //文件上传路径
        final String UPLOAD_PATH_PREFIX = "static/uploadFile/";
        String realPath = new String("client/src/main/resources/" + UPLOAD_PATH_PREFIX);
        //存放上传文件的文件夹
        File file = new File(realPath);
        if (!file.isDirectory()) {
            //递归生成文件夹
            file.mkdirs();
        }
        //获取原始的名字  original:最初的，起始的  方法是得到原来的文件名在客户机的文件系统名称
        String oldName = img.getOriginalFilename();
        String newName = UUID.randomUUID().toString() + oldName.substring(oldName.lastIndexOf("."), oldName.length());
        try {
            //构建真实的文件路径
            File newFile = new File(file.getAbsolutePath() + File.separator + newName);
            //转存文件到指定路径，如果文件名重复的话，将会覆盖掉之前的文件,这里是把文件上传到 “绝对路径”
            img.transferTo(newFile);
            //report表 修改状态,填写备注,添加图片
            return iReportService.alterReport(newName, reportid, remark);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new ResultBean(0, "图片上传失败,请选择清晰图片", 0);
    }

    //查看所有状态为待审核的案件
    @RequestMapping("/getAllReport")
    public ResultBean getAllReport() {
        return iReportService.getAllReport();
    }

    //审核状态初始化分页
    @GetMapping("firstReportPage")
    public PageBean<TReport> firstReportPage() {
        return iReportService.seachPage();
    }

    //审核状态分页查询
    @GetMapping("/getAllReportPage")
    public PageBean<TReport> getAllReportPage(int pageIndex, int pageSize) {
        return iReportService.seachPage(pageIndex, pageSize);
    }

    //报案号查询
    @RequestMapping("/getReportbyReportid")
    public PageBean<TReport> getReportbyReportid(Integer pageIndex, Integer pageSize, @RequestBody TReport tReport) {
        return iReportService.getReportbyReportid(pageIndex, pageSize, tReport);
    }

    //查看所有状态为待复审的案件
    @RequestMapping("/getAllReportByStates")
    public PageBean<TReport> getAllReportByStates(Integer pageIndex, Integer pageSize, @RequestBody TReport tReport) {
        return iReportService.getAllReportByStates(pageIndex, pageSize, tReport);
    }


}
